import { Component, OnInit } from '@angular/core';

import { MonitorShopComponentService } from './monitor.shop.component.service';

import { ShopBase } from '../../shop/shop.base';

import { Util } from '../../../utils/util';


@Component({
  selector: 'monitor-shop',
  templateUrl: './monitor.shop.component.html',
  styleUrls: ['./monitor.shop.component.css'],
  providers: [MonitorShopComponentService]
})

export class MonitorShopComponent extends ShopBase {

  constructor(public util: Util, public service: MonitorShopComponentService) {
    super(util, service);
    this.getHttp(this.param);
  }
  // 参数
  param: any = {
    type: 2,
    start: 0,
    rows: 1000,
    sort: '',
    sortType: ''
  };

  p: number = 0;
  // 列
  public columns: Array<any> = [
    {
      title: '店铺名称',
      name: 'brandName',
      sort: false
    },
    {
      title: '店铺等级',
      name: 'shopLevel',
      class: 'levelTh text-overflow',
      sort: true
    },
    {
      title: '动销宝贝数',
      name: 'productCount',
      sort: true,
      class: "text-overflow"
    },
    {
      title: '日销售额',
      name: 'sellMoney',
      sort: true,
      class: "text-overflow"
    },
    {
      title: '日销售量',
      name: 'sellAmount',
      sort: true,
      class: "text-overflow"
    },
    {
      title: '近30天销售额',
      name: 'monthSellMoney',
      sort: true,
      class: "text-overflow"
    },
    {
      title: '近30天销售量',
      name: 'monthSellAmount',
      sort: true,
      class: "text-overflow"
    }
  ];

  // ngOnInit(){

  // }

  addView: boolean = false;
  add() {
    this.addView = !this.addView;
    if (this.addView == false) {
      this.getHttp(this.param);
    }
  }

  sortChange(sort) {
    console.log(sort);

    let _sort: string = sort.sort;

    let _sortType: string = sort.sortType;

    if (_sortType == 'desc') {

      this.vm.table.shops.sort((a, b) => {
        return a[_sort] < b[_sort];
      });

    } else {

      this.vm.table.shops.sort((a, b) => {
        return a[_sort] > b[_sort];
      });

    }

  }

  export() {


    let dim = ['modifyTime', 'shopName', 'mainCategory', 'shopCreateTime', 'uid', 'shopType', 'loc', 'sellMoney', 'sellAmount', 'monthSellMoney', 'monthSellAmount', 'productCount'];

    let dimText = ['时间', '店铺名', '主营类目', '开店时间', '店铺id', '店铺类型', '地址', '日销售额', '日销售量', '近30天销售额', '近30天销售量', '动销宝贝数'];



    let fileName = "全部监控店铺";

    let data = this.util.jsonToArrry(this.vm.table.shops, dim, dimText);

    this.util.export(data, fileName);


  }

  getHttp(param) {
    var res = {
      "code": 0, "msg": "成功", "data": {
        "value":
          [
            { "id": "725677994", "uid": "725677994", "nick": "天猫超市", "sid": "67597230", "shopType": "商城", "shopName": "天猫超市", "shopCreateTime": "2011-06-28 14:48:53.0", "mainCategory": "百货/餐具/家庭保健--洗护清洁剂/卫生巾/纸/香薰", "shopLevel": 20, "loc": "上海", "province": "上海", "city": null, "monthSellAmount": 109997894, "monthSellMoney": 3.56287788257E9, "sellAmount": 3726328, "sellMoney": 9.81308616E7, "productCount": 35105, "modifyTime": "2017-12-04 05:08:26.0", "ifashion": 0, "date": null, "reason": null }], "count": 1
      }
    };

    if (res.data.value.length > 0) {
      for (let i in res.data.value) {
        let item = res.data.value[i];
        item = this.util.shopLevels(item);
      }
    }


    this.vm.table.shops = res.data.value;

    this.vm.table.count = res.data.count;



  }
}
